package com.tau.egonetwork.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.common.VisualizationPreferencesFile;
import com.tau.egonetwork.gui.core.BasicDialog;

public class VisualizationPreferencesDialog extends BasicDialog {

	private Button alertRadioButton;
	private Combo displayStatisticsCombo;
	private Button showVerticesAsFadedButton;
	private Button showBoldEdgesButton;
	private Combo showEdgesCombo;
	
	
	public VisualizationPreferencesDialog(Shell parent) {
		super(parent);
	}

	@Override
	protected void createWindowContents() {
		
		shell.setText("Visualization Preferences");
		
		//load existing preferences:
		boolean preferencesFileExists
			= VisualizationPreferencesFile.isVisualizationPreferencesFileExists();
		
		Composite preferencesComposite = new Composite(shell, SWT.NONE);
		preferencesComposite.setLayout(new GridLayout(1, true));
		preferencesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
						
		alertRadioButton = new Button(preferencesComposite, SWT.CHECK);
		alertRadioButton.setText("Alert me when less than 4 groups have been chosen in 'Priorotize Groups'.");
		if (preferencesFileExists)
			alertRadioButton.setSelection(VisualizationPreferencesFile.getAlertMeWhenUnder4());
		
		Composite displayStatisticsComposite = new Composite(preferencesComposite, SWT.NONE);
		displayStatisticsComposite.setLayout(new GridLayout(2, false));
		displayStatisticsComposite.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, false));
		
		Label displayStatisticsLabel = new Label(displayStatisticsComposite, SWT.NONE);
		displayStatisticsLabel.setText("Display Statistics: ");
		
		displayStatisticsCombo = new Combo(displayStatisticsComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		displayStatisticsCombo.addListener(SWT.Selection, comboValuesChangedListener);
		displayStatisticsCombo.add(VisualizationPreferencesFile.DISPLAY_STATISTICS_HOVER);
		displayStatisticsCombo.add(VisualizationPreferencesFile.DISPLAY_STATISTICS_CLICK);
		if (preferencesFileExists) {
			String selection = VisualizationPreferencesFile.getDisplayStatistics();
			if (selection.equals(VisualizationPreferencesFile.DISPLAY_STATISTICS_HOVER))
				displayStatisticsCombo.select(0);
			else
				displayStatisticsCombo.select(1);
		}
		
		Composite separatorComposite = new Composite(preferencesComposite, SWT.NONE);
		GridLayout separatorCompositeGridLayout = new GridLayout(1, true);
		separatorCompositeGridLayout.marginWidth = 0;
		separatorCompositeGridLayout.marginLeft = 0;
		separatorCompositeGridLayout.marginRight = 0;
		separatorComposite.setLayout(separatorCompositeGridLayout);
		separatorComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		
		Label separator = new Label(separatorComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData separatorGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		separatorGridData.horizontalSpan = 2;
		separator.setLayoutData(separatorGridData);
		
		Label whenShowingClusters = new Label(preferencesComposite, SWT.NONE);
		whenShowingClusters.setText("When Showing Clusters: ");
		Font titleFont = new Font(shell.getDisplay(), "Ariel", 9, SWT.BOLD);
		whenShowingClusters.setFont(titleFont);
		
		showVerticesAsFadedButton = new Button(preferencesComposite, SWT.CHECK);
		showVerticesAsFadedButton.setText("Show vertices not present in any cluster as faded.");
		if (preferencesFileExists)
			showVerticesAsFadedButton.setSelection(VisualizationPreferencesFile.getShowVerticesNotInAnyCluster());
		
		showBoldEdgesButton = new Button(preferencesComposite, SWT.CHECK);
		showBoldEdgesButton.setText("Show cluster edges between vertices as bold");
		if (preferencesFileExists)
			showBoldEdgesButton.setSelection(VisualizationPreferencesFile.getShowClusterEdgesAsBold());
		
		Composite showEdgesComposite = new Composite(preferencesComposite, SWT.NONE);
		showEdgesComposite.setLayout(new GridLayout(2, false));
		showEdgesComposite.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, false));
		
		Label showEdgesLabel = new Label(showEdgesComposite, SWT.NONE);
		showEdgesLabel.setText("Show edges not in cluster: ");

		showEdgesCombo = new Combo(showEdgesComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
		showEdgesCombo.addListener(SWT.Selection, comboValuesChangedListener);
		showEdgesCombo.add(VisualizationPreferencesFile.SHOW_NON_CLUSTER_EDGES_FADED);
		showEdgesCombo.add(VisualizationPreferencesFile.SHOW_NON_CLUSTER_EDGES_DONT_SHOW);
		if (preferencesFileExists) {
			String selection = VisualizationPreferencesFile.getShowEdgesNotInCluster();
			if (selection.equals(VisualizationPreferencesFile.SHOW_NON_CLUSTER_EDGES_FADED))
				showEdgesCombo.select(0);
			else
				showEdgesCombo.select(1);
		}

		
		setOKButtonEnabled(preferencesFileExists);
	}

	Listener comboValuesChangedListener = new Listener() {
		
		@Override
		public void handleEvent(Event arg0) {
			int display = displayStatisticsCombo.getSelectionIndex();
			int show = showEdgesCombo.getSelectionIndex();
			setOKButtonEnabled(display >= 0 && show >= 0);
		}
	};
	
	@Override
	protected Object setReturnValue() {
		//nothing to return for this dialog
		return null;
	}

	@Override
	protected void onOKClicked() {
		VisualizationPreferencesFile.savePreferences(
				alertRadioButton.getSelection(), 
				displayStatisticsCombo.getText(),
				showVerticesAsFadedButton.getSelection(),
				showBoldEdgesButton.getSelection(),
				showEdgesCombo.getText());
		shell.close();
	}

}
